package zmee.pur.src.formplugin;

import com.alibaba.druid.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.ListFilterParameter;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import zmee.util.ShowFormUtils;

import java.io.IOException;
import java.util.EventObject;

/**
 * @Author: zyj
 * @Date: 2023/6/20 14:16
 * @Description: 供应商: 增加采购信息按钮查询支出合同采购信息，供展示
 */
public class SrmSupplierPurchasingDataBillPlugin extends AbstractBillPlugIn {

    // 采购数据按钮标识
    private static final String BUTTON_KEY_PURCHASINGDATA = "zmee_purchasingdata";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 监听历史采购信息数据工具栏
        this.addItemClickListeners("zmee_historicalpurtoolbar");
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        if (StringUtils.equalsIgnoreCase(BUTTON_KEY_PURCHASINGDATA, evt.getItemKey())) {
            try {
                // 内嵌基本信息
                this.showEmbedPage("pmct_outcontract", "zmee_purchasingdetail");
                this.getView().updateView();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 内嵌页面
     *
     * @param pageNumber 页面标识
     * @param targetKey 加到当前页面哪个容器下
     * @throws IOException
     */
    private void showEmbedPage(String pageNumber, String targetKey) throws IOException {
        ListShowParameter contactParameter = ShowFormUtils.inContainerListParameter(pageNumber, targetKey);
        // 供应商编码
        Object number = this.getModel().getValue("number");
        if (number == null) {
            this.getView().showTipNotification("请先填写供应商编码!");
            return;
        }
        // 设置过滤条件 根据供应商去过滤支出合同合同乙方
        QFilter qFilter = new QFilter("partb.number", QCP.equals, number);
        ListFilterParameter filterParam = new ListFilterParameter();
        filterParam.setFilter(qFilter);
        contactParameter.setListFilterParameter(filterParam);
        // 设置自定义参数 表示从供应商界面跳转的支出合同 隐藏相应字段(该方法在支出合同列表插件中实现)
        contactParameter.setCustomParam("state", "supplier");
        // 设置查询范围合同清单1
        contactParameter.setSelectedEntity("sublistentry");
        this.getView().showForm(contactParameter);
    }
}
